package com.yunsc.admin.controller.store;

import com.alibaba.fastjson.JSON;
import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.store.dto.QueryPageDTO;
import com.yunsc.store.dto.StoreExpressDTO;
import com.yunsc.store.dto.StoreSettingDTO;
import com.yunsc.store.entity.YunscExpressEntity;
import com.yunsc.store.entity.YunscPrinterEntity;
import com.yunsc.store.entity.YunscStoreSettingEntity;
import com.yunsc.store.service.YunscExpressService;
import com.yunsc.store.service.YunscPrinterService;
import com.yunsc.store.service.YunscStoreSettingService;
import com.yunsc.store.vo.SMSNoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 商家设置信息接口
 * @date: 2024-04-30 14:22
 */
@Api(value = "商家设置信息接口", tags = "商家信息接口")
@Slf4j
@RestController
@RequestMapping("setting")
public class StoreSettingController {

    @Resource
    private YunscStoreSettingService storeSettingService;
    @Resource
    private YunscPrinterService printerService;
    @Resource
    private YunscExpressService expressService;


    @ApiOperation("分类模板")
    @GetMapping("/detail")
    public Result<?> queryRegionInfo(@RequestParam(value = "key", required = false) String key) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        if ("sms".equals(key)){
            SMSNoticeVO smsNoticeVO = storeSettingService.querySMSSettingVO(currentStoreId);
            Map<String, Object> map = new HashMap<>();
            map.put("values", smsNoticeVO);
            return Result.ok(map);
        }
        YunscStoreSettingEntity storeSetting = storeSettingService.queryStoreSettingByKey(key, currentStoreId);
        Map<String, Object> map = new HashMap<>();
        map.put("values", JSON.parseObject(storeSetting.getValues(), Map.class));
        return Result.ok(map);
    }


    @ApiOperation("保存分类模板")
    @PostMapping("/update")
    public Result<?> updateStoreSetting(@RequestBody StoreSettingDTO params) {
        storeSettingService.updateStoreSetting(params);
        return Result.ok();
    }

    @ApiOperation("查新物流接口")
    @GetMapping("/express/all")
    public Result<?> queryExpressAll() {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        List<YunscExpressEntity> entities = storeSettingService.queryExpressAll(storeId);
        return Result.resultList(entities);
    }



    @ApiOperation(value = "打印列表", notes = "打印列表")
    @RequestMapping("/printer/all")
    public Result<?> queryPrinterAll(){
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        List<YunscPrinterEntity> printerList = printerService.queryPrinterAll(currentStoreId);
        return Result.resultList(printerList);
    }

    @ApiOperation(value = "获取快递公司列表", tags = "StoreLoginController")
    @GetMapping("/express/list")
    public Result<?> getExpressList(QueryPageDTO param) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscExpressEntity> expressList = expressService.queryExpressPage(param, storeId);
        return Result.resultPageList(expressList);
    }

    // /express/add
    @ApiOperation(value = "添加快递公司", tags = "StoreLoginController")
    @PostMapping("/express/add")
    public Result<?> addExpress(@RequestBody StoreExpressDTO params) {
        Integer storeId = SecurityUtil.getCurrentStoreId();
        expressService.addExpress(params, storeId);
        return Result.ok("添加成功");
    }

    // /express/delete

    @ApiOperation(value = "删除快递公司", tags = "StoreLoginController")
    @PostMapping("/express/delete/{id}")
    public Result<?> deleteExpress(@RequestBody @Validated(ValidationGroups.Delete.class) StoreExpressDTO params) {
        expressService.deleteExpress(params.getExpressId());
        return Result.ok("删除成功");
    }

    // /express/edit
    @ApiOperation(value = "编辑快递公司", tags = "StoreLoginController")
    @PostMapping("/express/edit")
    public Result<?> editExpress(@RequestBody @Validated(ValidationGroups.Update.class) StoreExpressDTO params) {
        expressService.editExpress(params);
        return Result.ok("编辑成功");
    }

}
